#include "project.h"

bool DEVICE::Dev_Init()
{
    if ((this->dev_fd = open(this->dev_file_path.c_str(), O_RDWR)) == -1)
    {
        cerr << "open" << endl;
        return false;
    }
    return true;
}

bool DEVICE::Dev_Free()
{
    if (close(this->dev_fd) == -1)
    {
        cerr << "close" << endl;
        return false;
    }

    return true;
}


bool LCD::Lcd_Star()
{
    // ioctl 获取LCD 的分辨率 1024 X 600

    struct fb_var_screeninfo vinfo;
    if (ioctl(this->DEVICE::dev_fd, FBIOGET_VSCREENINFO, &vinfo) < 0)
    {
        cout << "获取相关信息失败" << endl;
        close(this->DEVICE::dev_fd);
    }

    this->screen_wide = vinfo.xres;
    this->screen_high = vinfo.yres;

    int fb_size = this->screen_wide * this->screen_high * (vinfo.bits_per_pixel / 8);
    // mmap();
    this->mmap_ptr= static_cast<char *>(mmap(NULL, fb_size, PROT_WRITE,
                                               MAP_SHARED, this->DEVICE::dev_fd, 0));
    if (this->mmap_ptr == MAP_FAILED)
    {
        perror("Failed to map framebuffer device");
        close(this->DEVICE::dev_fd);
        return false;
    }

    bzero(this->mmap_ptr,this->screen_wide*this->screen_high*4);
    return true;
}